/** @type {import('next').NextConfig} */
const path = require('path')
const withLess = require('next-with-less')
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg|png)(\?.*)?$/i
const isProd = process.env.NODE_ENV === 'production'

const nextConfig = {
  output: isProd ? 'export' : 'standalone',
  trailingSlash: false,
  images: { unoptimized: true },
  poweredByHeader: false,
  compress: true,
  reactStrictMode: false, //是否使用严格模式
  productionBrowserSourceMaps: false,
  experimental: {},
  /* 构建输出目录 */
  distDir: isProd ? '.next' : '.next_dev',
  /* 静态资源前缀 */
  assetPrefix: isProd ? './' : undefined,
  /* 应用基础路径 */
  basePath: '',
  pageExtensions: ['tsx'],
  // async rewrites() {
  //   return [
  //     //接口请求 前缀带上/api-text/
  //     {
  //       source: '/web/:path*',
  //       destination: `https://wajib.adaa-test.datepalm.site/:path*`,
  //     },
  //   ]
  // },
  eslint: {
    // Warning: This allows production builds to successfully complete even if
    // your project has ESLint errors.
    ignoreDuringBuilds: true,
  },

  webpack: (config, { isServer }) => {
    config.resolve.alias['@'] = path.resolve(__dirname, 'src')
    config.module.rules.push({
      test: /\.svg$/,
      use: [
        {
          loader: '@svgr/webpack',
          options: {
            svgo: false, // 禁用 SVGO
            dimensions: false, // 禁用原始 SVG 外包围尺寸保留原始尺寸
            svgoConfig: {
              plugins: [
                { removeViewBox: false }, // 禁止移除 viewBox 属性
                { removeDimensions: true }, // 移除固定的尺寸
              ],
            },
            svgrOptions: {
              plugins: [{ removeViewBox: false }], // 也要确保 svgrOptions 中删除视图框
            },
          },
        },
      ],
    })
    if (isProd && !isServer) {
      config.plugins.push(
        new CompressionWebpackPlugin({
          algorithm: 'gzip',
          test: productionGzipExtensions,
          threshold: 10240,
          minRatio: 0.8,
          deleteOriginalAssets: false,
        })
      )
    }

    return config
  },
}

module.exports = withLess(nextConfig)
